package com.xiaoyu.lanling.feature.conversation

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.im.datamodel.IMOnlineStatus
import com.xiaoyu.im.event.IMOnlineStatusUpdateEvent
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.conversation.ConversationListUpdateEvent
import com.xiaoyu.lanling.feature.conversation.date.ConversationListData
import com.xiaoyu.lanling.feature.conversation.model.ConversationListItemBase
import com.xiaoyu.lanling.feature.conversation.viewholder.ConversationListViewHolderManager
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.fragment_main_conversation.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainConversationFragment : OnShowFragment() {

    val TAG = "MainConversationFragment"

    val adapter = CubeRecyclerViewSimpleAdapter<ConversationListItemBase>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return layoutInflater.inflate(R.layout.fragment_main_conversation, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)

        initAdapter()
        initBind()
        initEvent()
        initData()
    }

    override fun onDestroy() {
        super.onDestroy()
        AppEventBus.getInstance().removeStickyEvent(IMOnlineStatusUpdateEvent::class.java)
    }

    private fun initAdapter() {
        ConversationListViewHolderManager.bindViewHolder(adapter)
        recyclerview.adapter = adapter
    }

    private fun initBind() {
        swipe_refresh_layout.setOnRefreshListener {
            ConversationListData.instance.loadAllConversation()
        }
        friend.setOnClickDebounceListener {
            activity?.let {
                Router.instance.gotoFriendListActivity(it)
            }
        }
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: ConversationListUpdateEvent) {
                swipe_refresh_layout.isRefreshing = false
                val list = ArrayList<ConversationListItemBase>(ConversationListData.instance.list)
                adapter.list = list
                adapter.notifyDataSetChanged()
            }

            @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
            fun onEvent(event: IMOnlineStatusUpdateEvent) {
                updateTitleViewForNim(event.status)
            }
        })
    }

    private fun initData() {
        ConversationListData.instance.loadAllConversation()
        swipe_refresh_layout.isRefreshing = true
    }

    private fun updateTitleViewForNim(status: String) {
        when (status) {
            IMOnlineStatus.CONNECTING -> title.setText(R.string.main_chat_title_connecting)
            IMOnlineStatus.SYNCING -> title.setText(R.string.main_chat_title_syncing)
            IMOnlineStatus.LOGINING -> title.setText(R.string.main_chat_title_logining)
            IMOnlineStatus.LOGINED -> title.setText(R.string.main_message_title)
            else -> title.setText(R.string.main_chat_title_offline)
        }
    }
}